package email;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;

import settings.MasterController;

import dataStorage.DataHandler;
import dataStorage.DataXML;
import entity.Settings;

//@author U096819H
public class EmailManager {

	private Settings settings = new Settings();
	private String systemEmail;
	private String systemPassword;

	// @author U096819H
	public EmailManager() throws TransformerConfigurationException,
			InstantiationException, IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		getObject();
		this.systemEmail = "ezxpens.t40@gmail.com";
		this.systemPassword = "cs2103t40";
	}

	// @author U096819H
	public boolean isActive() {
		return settings.getMailActive();
	}

	// @author U096819H
	public boolean deactivate() throws TransformerConfigurationException,
			JAXBException, ParserConfigurationException, SAXException,
			IOException, TransformerException, InstantiationException,
			IllegalAccessException {
		if (settings.getMailActive()) {
			settings.setMailActive(false);
			DataHandler dataHandler = DataXML.getInstance();
			dataHandler.update("today", settings, "settings", Settings.class);
		}
		return false;
	}

	// @author U096819H
	public boolean activate(String email, String password)
			throws TransformerConfigurationException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, InstantiationException,
			IllegalAccessException {
		if (!settings.getMailActive()) {
			// validate mail
			settings.setMailActive(true);
			settings.setEmail(email);
			settings.setPassword(password);
			DataHandler dataHandler = DataXML.getInstance();
			dataHandler.update("today", settings, "settings", Settings.class);
			return true;
		}
		return false;
	}

	// @author U096819H
	private void getObject() throws TransformerConfigurationException,
			InstantiationException, IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		DataHandler dataHandler = DataXML.getInstance();
		ArrayList<Settings> list = dataHandler.load("settings", Settings.class);
		settings = (Settings) list.get(0);
	}

	// @author U096819H
	public boolean send(String to, String subject, String msg) {
		if (isActive()) {
			SendMail sm = new SendMail(settings.getEmail(),
					settings.getPassword());
			sm.sendEmail(to, subject, msg);
			return true;
		} else {
			return false;
		}
	}

	// @author U096819H
	public boolean receive(MasterController mc) {
		if (isActive()) {
			String checkForUser = settings.getEmail();
			ReceiveMail rm = new ReceiveMail(systemEmail, systemPassword,
					checkForUser, mc);
			rm.checkMail();
			return true;
		} else {
			return false;
		}

	}
}
